<%@page import="cn.edu.nju.ws.sview.rdf.URIResource"%>
<%@page import="cn.edu.nju.ws.sview.util.URIUtil"%>
<%@page import="cn.edu.nju.ws.sview.cache.URILabelCache"%>
<%@page import="cn.edu.nju.ws.sview.Config,cn.edu.nju.ws.sview.cache.URIIDCache"%>
<%@page import="cn.edu.nju.ws.sview.views.Entity"%>
<%@page import="cn.edu.nju.ws.sview.views.data.EntityManagement"%>
<%@page import="cn.edu.nju.ws.sview.views.Bookmark"%>
<%@page import="cn.edu.nju.ws.sview.views.Group"%>
<%@page import="cn.edu.nju.ws.sview.views.User"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>收藏管理</title>
<link rel="icon" href="/sview/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/sview/css/views/bmmanage.css" />
	<link rel="stylesheet" type="text/css" href="/sview/css/layout.css">
<link rel="stylesheet" type="text/css" href="/sview/css/easyui.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/smoothness/jquery-ui-1.8.16.custom.css" />
	<script type="text/javascript" src="/sview/script/jquery.js"></script>
<script type="text/javascript" src="/sview/script/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/sview/script/jquery-ui-1.8.16.custom.min.js"></script>
	<script type="text/javascript" src="/sview/script/global.js"></script>
<script type="text/javascript" src="/sview/script/views/bmmanage.js"></script>
  </head>
  <%
  	String lang = request.getParameter("lang");
           if (lang != null && lang.equals("en")) {
    	request.getRequestDispatcher("bm_manage.jsp").forward(
    			request, response);
    		} else if (lang == null) {
    	lang = "zh";
    		}
           String from = request.getParameter("from");
           
        	String uname = (String) session.getAttribute("user"); 
        	if(uname == null){
        		uname = (String) request.getParameter("user");
        		request.getSession().setAttribute("user", uname);
        	}
        	User user = new User(uname);
        	
        	if(uname == null){
        		request.getRequestDispatcher("../views/index_zh.jsp").forward(request, response);
        	}else{
        	
    		ArrayList<Integer> ebmIds = Bookmark.getBookmarkIdsbyUserId(User.getUserIdByName(uname));
  %>
  <body>
<jsp:include page="/vheader_zh.jsp" flush="true">
   <jsp:param name="f" value="e"/>
</jsp:include>
    <div id="container" class="container">
	    <div class="gHeader"><span class="large">收藏</span></div>
	    <div id="gmanage_ection" class="section">
		    <div class="tabDiv">
		    	<ul class="tabUl">
		    		<li id="entitybmList" class="tabLi highlighted" onClick="clickTab();">实体收藏</li>
		    		<li id="datasetbmList" class="tabLi faded" onClick="clickTab();">数据集收藏</li>
		    	</ul>
		    </div>
	    	<div class="content">
				<div class="entitybmList tableDiv" id="entitybmList">
				<table>
		    	<thead>
		    		<tr>
		    			<td class="select">选择</td>
		    			<td>收藏名</td>
		    			<td>实体</td>
		    		</tr>
		    	</thead>
		    	<tbody>
				<%
					if(ebmIds != null && ebmIds.size()>0){
		    		for(Integer bmId : ebmIds){ 
		    			String bmname = Bookmark.getNameByBookmarkID(bmId);
		    			ArrayList<Integer> memList = Bookmark.getMembersByBookmarkId(bmId);
		    			String elabel = null;
		    			String euri = null;
		    			int eid = -1;
		    			int etype = -1;
		    			if(memList.size() == 2){
		    			    etype = memList.get(0);
		    			    eid = memList.get(1);
		    			}
		    			String redirect = null;
		    			String type_str = URIUtil.getItemTypeStr(etype);
		    			String td_class = "bmdirect "+type_str;
		    			if(etype==Entity.URI_ENTITY){
		    				redirect = "/sview/entity/o"+eid;
		    				try {
		    					elabel = URIResource.getLabel(eid, lang);//URILabelCache.getInstance().getLabel(eid, lang);
		    				//	euri = URIIDCache.getInstance().getURI(eid);
		    				} catch (Throwable e) {
		    					e.printStackTrace();
		    				}
		    				System.out.println("o:"+elabel+"|"+(elabel == null || elabel.trim().length()<=0));
		    				if(elabel == null || elabel.trim().length()<=0){
		    					elabel = "(uri entity)";
		    				}
		    			}else if(etype==Entity.CUSTOM_ENTITY){ 
		    				redirect = "/sview/entity/e"+eid;
		    				elabel = Entity.getNameByEntityID(eid, lang);//Entity.getNameByEntityID(eid);
		    				if(elabel == null || elabel.trim().length()<=0){
		    					elabel = "(custom entity)";
		    				}
		    			}else if(etype==Entity.COLLECTION){
		    				redirect = "/sview/entity/c"+eid;
		    				elabel = Entity.getNameByEntityID(eid, lang);//=======================
		    				if(elabel == null || elabel.trim().length()<=0){
		    					elabel = "(entity collection)";
		    				}
		    			}else{
		    				Bookmark.delete(bmId);
		    				continue;
		    			} 
		    			redirect += "&lang="+lang;
		    			%>
					<tr id=<%=bmId%>>
						<td class="select"><input type="checkbox" class="checkbox"/></td>
		    			<td class="bmname"><%=bmname%></td>
		    			<td class="<%=td_class %>"><a href="<%=redirect %>"><%=elabel %></a></td>
					</tr>
				<%}
				}%>
		    	</tbody>
		    	</table>
		    	<%if(ebmIds != null && ebmIds.size()>0){ %>
		    		<input type="button" class="form-button-little" id="delEBM" value="删除" style="float:right; margin-top:50px;" onClick="delEBM();" />
				<%} %>
				</div>
				<div class="tableDiv datasetbmList" id="datasetbmList" style="display: none;">
				<table>
			    	<thead>
			    		<tr>
		    				<td class="select">选择</td>
			    			<td>收藏名</td>
			    			<td>实体</td>
			    		</tr>
			    	</thead>
			    	<tbody>
			    	</tbody>
			  		</table>
					<input type="button" class="form-button-little" id="delDSBM" value="删除" style="float:right; margin-top:50px;" onClick="delDSBM();" />
				</div>
				
			</div>
	    </div>
    </div>
		<input type="hidden" id="display-lang" value="zh" />
		<jsp:include page="/vfooter_zh.jsp"></jsp:include>
  </body>
  <%} %>
</html>
